<template>
	<view class="home">
		<view class="top">
			<my-search @click="toSearch" bgColor="#eb4450" radius="10"></my-search>
		</view>
		<!-- 轮播图 -->
		<swiper class="myswiper" indicator-dots autoplay circular interval="2000" duration="200">
			<swiper-item v-for="(item, index) in swiperList" :key="index">
				<navigator :url="item.navigator_url">
					<image :src="item.image_src" mode="aspectFit"></image>
				</navigator>
			</swiper-item>
		</swiper>
		<!-- 分类 -->
		<view class="cate-box">
			<view class="cate-item" @click="navTo(item.name)" v-for="(item, index) in cateList" :key="index">
				<image :src="item.image_src"></image>
			</view>
		</view>
		<!-- 楼层 -->
		<view class="floor">
			<view class="floor-item" v-for="(item, index) in floorList" :key="index">
				<!-- 标题栏 -->
				<view class="title">
					<image :src="item.floor_title.image_src" mode="aspectFit"></image>
				</view>
				<!-- 产品 -->
				<view class="prod">
					<view class="left">
						<image :src="item.product_list[0].image_src"
							:style="{ width: item.product_list[0].image_width + 'rpx' }"></image>
					</view>
					<view class="right">
						<block v-for="(subitem, subindex) in item.product_list">
							<image v-if="subindex !== 0" :key="subindex" :src="subitem.image_src"
								:style="{ width: subitem.image_width + 'rpx' }"></image>
						</block>
					</view>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	import mymixins from '../../mixins/mymixins.js'
	export default {
		// 使用混入
		mixins: [mymixins],
		data() {
			return {
				// 轮播图的数据源
				swiperList: [],
				// 分类的数据源
				cateList: [],
				// 产品的数据源
				floorList: [],
			};
		},
		onLoad() {
			// 获取轮播图的数据
			this.getSwiperList(),
				this.getCateList(),
				this.getFloorList()
		},
		methods: {
			toSearch() {
				uni.navigateTo({
					url: '/pages/search/search'
				})
			},
			// 获取轮播图的数据源
			async getSwiperList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/home/swiperdata')
				// 判断返回的状态
				if (res.meta.status !== 200) return uni.$showMsg()
				// if (res.meta.status !== 200) {
				// 	return uni.showToast({
				// 	  title: res.meta.msg,
				// 	  icon: "error"
				// 	})
				//    }
				// 保存数据
				this.swiperList = res.message
			},
			async getCateList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/home/catitems')
				// console.log(res);
				if (res.meta.status !== 200) return uni.$showMsg()
				// 保存数据
				this.cateList = res.message
			},
			// 点击导航进行跳转
			navTo(name) {
				// 区别是否是分类
				if (name === '分类') {
					uni.switchTab({
						url: '/pages/cate/cate'
					})
				}

			},
			async getFloorList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/home/floordata')
				// console.log(res);
				if (res.meta.status !== 200) return uni.$showMsg()
				// 保存数据
				this.floorList = res.message
			}
		}
	}
</script>

<style lang="scss">
	.top {
		position: sticky;
		left: 0;
		top: 0;
		z-index: 999;

	}

	.myswiper {
		height: 340rpx;

		image {
			width: 100%;
			height: 340rpx;
		}

		swiper-item {}
	}

	.cate-box {
		display: flex;
		justify-content: space-around;
		align-items: center;
		height: 194rpx;

		.cate-item {
			width: 25%;
			display: flex;
			justify-content: space-around;
			align-items: center;

			image {
				width: 128rpx;
				height: 140rpx;
			}

		}
	}

	.floor {
		.floor-item {
			.title {
				background-color: #f4f4f4;
				height: 88rpx;
				margin: 20rpx 0;

				image {
					height: 100%;
				}
			}

			.prod {
				border: 10rpx;
				display: flex;

				.left {
					height: 480rpx;
					margin-right: 10rpx;
				}

				.right {
					display: flex;
					flex-wrap: wrap;
					align-items: space-between;

					image {
						height: 235rpx;
						margin-right: 10rpx;
						margin-bottom: 10rpx;

						&:nth-child(n + 3) {
							margin-bottom: 0;
						}
					}

				}
			}
		}
	}
</style>
